package leetcode.offer;

/**
 * ClassName: Offer16_MyPow
 * Description: leetcode-剑指offer 16 数值的整数次方
 * date: 2021-11-15 22:52
 *
 * @author liyifan
 */
public class Offer16_MyPow {
    public double myPow(double x, int n) {
        if (n == 0) {
            return 1.0;
        }
        if (n < 0) {
            return 1 / calc(x, -n);
        }
        return calc(x, n);
    }

    public double calc(double x, int n) {
        if (n == 1) {
            return x;
        }
        if (n == 0) {
            return 1;
        }
        double ans = 1;
        if (n % 2 != 0) {
            ans = x;
        }
        return ans * calc(x * x, n / 2);
    }
}
